<template>
  <div class="loginContainer">
    <el-form
      ref="logindata"
      :rules="loginRules"
      :model="logindata"
      class="loginBox"
    >
      <h3 class="logintitle">系统登录</h3>
      <el-form-item prop="username">
        <el-input
          type="text"
          v-model="logindata.username"
          placeholder="请输入用户名"
          prefix-icon="el-icon-s-custom"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          v-model="logindata.password"
          placeholder="请输入密码"
          prefix-icon="el-icon-lock"
        ></el-input>
      </el-form-item>
      <el-checkbox v-model="remenber">记住我</el-checkbox>
      <el-form-item>
        <el-button
          type="primary"
          style="width: 73%; margin-top: 20px"
          @click="submit"
          >登录</el-button
        >
        <el-button @click="resetForm('logindata')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      //表单的绑定数据
      logindata: {
        username: 'admin',
        password: '123456',
      },
      //表单验证
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在3到10个字符之间',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在3到10个字符之间',
            trigger: 'blur',
          },
        ],
      },
      remenber: true,
    }
  },
  methods: {
    //登录提示消息
    submit() {
      this.$refs.logindata.validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.axios.post('login', this.logindata)
          if (res.meta.status !== 200) {
            return this.$message.error('登录失败')
          } else {
            this.$message({
              message: '登录成功',
              type: 'success',
            })
          }
          window.sessionStorage.setItem('token', res.data.token)
          this.$router.push('/home')
          console.log(this)
        } else {
          this.$message.error('请输入所有信息')
          return false
        }
      })
    },
    //重置按钮方法
    resetForm() {
      console.log(this)
      this.$refs.logindata.resetFields()
    },
  },
}
</script>

<style>
.loginContainer {
  /* background-color: #2b4b6b; */
  height: 100%;
}
.loginBox {
  border-radius: 15px;
  /* 背景被裁减到内边距框 */
  background-clip: padding-box;
  margin: 180px auto;
  width: 300px;
  padding: 15px 35px 15px 35px;
  background-color: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}
.logintitle {
  margin-left: 100px;
}
</style>
